const entity = require("./entity");
const fs = require("fs");
const path = require("path");
let conowPath = require('./moduleHelper').conowPath;
let entityFilePath = path.join(__dirname,'../templ/entity.temp');
let outputPath = path.join(__dirname,'./output');
//读取文件
let files = fs.readFileSync(entityFilePath,{encoding:'utf-8'});

const templRender = new require('../tools/templRenderUtil');




let entityPath = path.join(conowPath,'./entity');

if(!fs.existsSync(entityPath)){
	fs.mkdirSync(entityPath);
}


exports.ramdom = function(entity){
	let entityUpName = entity.name.charAt(0).toUpperCase()+entity.name.slice(1);
	let entitySubName = entity.name.charAt(0).toLowerCase()+entity.name.slice(1);
	let defaultFields = ["ID","UPDATER","CREATER","UPDATE_TIME","CREATE_TIME","CORP_ORG_ID"];
	if(entity.hasWorkflow){
		defaultFields.push('STATE');
	}
	let outputData = files.replace(/\${entity.package}/g,`cn.conow.${entity.module}.entity`);
	outputData = outputData.replace(/\${entity.upName}/g,entityUpName);
	outputData = outputData.replace(/\${entity.tableName}/g,entity.tableName);
	outputData = outputData.replace(/\${entity.author}/g,entity.author);
	outputData = outputData.replace(/\${entity.version}/g,entity.version);
	outputData = outputData.replace(/\${entity.getSetColumn}/g,(data)=>{
		let str = "";
		for(let c in entity.column){
			if(defaultFields.indexOf(c.toUpperCase())>-1){
				continue;
			}
			str += `
		// ${entity.column[c].comment}
		private ${entity.column[c].type} ${c};

		public void set${c.charAt(0).toUpperCase()+c.slice(1)}(${entity.column[c].type} ${c}){
			this.${c} = ${c};
		}

		public ${entity.column[c].type} get${c.charAt(0).toUpperCase()+c.slice(1)}(){
			return this.${c};
		}


					`;
		}

		str+=`
		// 创建人
		private String CREATER;

		public void setCREATER(String CREATER) {
			this.CREATER = CREATER;
		}

		public String getCREATER() {
			return this.CREATER;
		}

		// 创建时间
		private Date CREATE_TIME;

		public void setCREATE_TIME(Date CREATE_TIME) {
			this.CREATE_TIME = CREATE_TIME;
		}

		public Date getCREATE_TIME() {
			return this.CREATE_TIME;
		}

		// 更新人
		private String UPDATER;

		public void setUPDATER(String UPDATER) {
			this.UPDATER = UPDATER;
		}

		public String getUPDATER() {
			return this.UPDATER;
		}

		// 更新时间
		private Date UPDATE_TIME;

		public void setUPDATE_TIME(Date UPDATE_TIME) {
			this.UPDATE_TIME = UPDATE_TIME;
		}

		public Date getUPDATE_TIME() {
			return this.UPDATE_TIME;
		}

		// 独立核算单位id
		private String CORP_ORG_ID;


		public void setCORP_ORG_ID(String CORP_ORG_ID) {
			this.CORP_ORG_ID = CORP_ORG_ID;
		}

		public String getCORP_ORG_ID() {
			return this.CORP_ORG_ID;
		}

		
		
		`;

		if(entity.hasWorkflow){
			str+=`

		// 状态
		private String STATE;

		public void setSTATE(String STATE) {
			this.STATE = STATE;
		}

		public String getSTATE() {
			return this.STATE;
		}
			`;
		}
		return str;
	});
	outputData = templRender.renderTempl(outputData,entity);
	let fileOutputPath = path.join(entityPath,`./${entityUpName}.java`);
	//fs.writeFileSync(fileOutputPath, outputData);
	return {fileName:`${entityUpName}.java`,data:outputData};

}

/*// 密级
		private String SECRET_LEVEL;


		public void setSECRET_LEVEL(String SECRET_LEVEL) {
			this.SECRET_LEVEL = SECRET_LEVEL;
		}

		public String getSECRET_LEVEL() {
			return this.SECRET_LEVEL;
		}

		// 同步标识
		private String ETL_FLAG;


		public void setETL_FLAG(String ETL_FLAG) {
			this.ETL_FLAG = ETL_FLAG;
		}

		public String getETL_FLAG() {
			return this.ETL_FLAG;
		}*/